package com.example.onenetdht11;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.onenetdht11.dao.UserDao2;
import com.example.onenetdht11.entity.User2;

public class LoginActivity extends AppCompatActivity {
    Button confirm,btnRegister, btnChangePassword ;
    EditText etpassword,etusername;
    CheckBox cbRm;
    UserDao2 userDao;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userDao = new UserDao2(this);
        initView();
        initListener();
    }


    void initView(){
        cbRm = findViewById(R.id.cb_rm);
        confirm = findViewById(R.id.btn_confirm);
        btnRegister = findViewById(R.id.btn_register);
        btnChangePassword = findViewById(R.id.btn_changePassword);
        confirm.setEnabled(false);
        etpassword = findViewById(R.id.et_password);
        etusername = findViewById(R.id.et_username);

    }

    void initListener(){
        @SuppressLint("NonConstantResourceId") View.OnClickListener listener = v -> {
            switch (v.getId()){
                case  R.id.btn_confirm:
                    login();
//                        Contests.id = etusername.getText().toString();
                    Contests.id = etusername.getText().toString();
                    break;
                case R.id.btn_changePassword:
                    changePassword();
                    break;
                case R.id.btn_register:
                    register();
                    break;
            }
        };
        TextWatcher textlisterner = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                confirm.setEnabled(etpassword.length() > 0 && etusername.length() == 11);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        confirm.setOnClickListener(listener);
        btnRegister.setOnClickListener(listener);
        btnChangePassword.setOnClickListener(listener);
        etpassword.addTextChangedListener(textlisterner);
        etusername.addTextChangedListener(textlisterner);
    }

    private void changePassword() {
        gotoNextActivity(ChangePasswordActivity.class);
    }


    private void register() {
        gotoNextActivity(RegisterActivity.class);
    }

    /**
     * 用户登录
     */
//    private void login() {
//        new Thread() {
//            @Override
//            public void run() {
//                UserDao userDao = new UserDao();
//                boolean aa = userDao.login(etusername.getText().toString(), etpassword.getText().toString());
//                Looper.prepare();
//                if (aa == true){
//                    Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
//                    gotoNextActivity(MainActivity.class);
//                }else {
//                    Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();
//                }
//                Looper.loop();
//            }
//        }.start();
//    }
    private void login() {
        boolean login = userDao.selectByAccount(etusername.getText().toString(), etpassword.getText().toString());
        if (login){
            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
            User2 user = userDao.getUserByAccount(etusername.getText().toString(), etpassword.getText().toString());
            Contests.MaxTemp = user.getMaxtmp();
            Contests.MinTemp = user.getMintmp();
            gotoNextActivity(user);
        }
        else {
            Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 带着user对象跳转到下一个界面
     * @param user  要传递的user对象
     */
    void gotoNextActivity(User2 user){
        Intent intent_back = new Intent();
        intent_back.setClass(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("user",user);
        intent_back.putExtras(bundle);
        startActivity(intent_back);
        finish();
    }

    /**
     * 跳转到下一个界面
     * @param activity
     */
    void gotoNextActivity(Class activity){
        Intent intent_back = new Intent();
        intent_back.setClass(this, activity);
        startActivity(intent_back);
//        finish();
    }
}
